/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-03
 * Time: 21:52
 */
class Solution3 {
    public String addBinary(String a, String b) {
        StringBuilder ret = new StringBuilder();
        int cur1 = a.length()-1, cur2 = b.length()-1, t = 0;

        while(cur1 >= 0 || cur2 >= 0 || t != 0) {
            if(cur1 >= 0) {
                t += a.charAt(cur1--)-'0';
            }
            if(cur2 >= 0) {
                t += b.charAt(cur2--)-'0';
            }
            ret.append((char)('0'+(char)(t % 2)));
            t /= 2;
        }

        ret.reverse();
        return ret.toString();
    }
}